package com.jungle.mvvm.sample.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.databinding.ObservableField;
import android.net.Uri;
import android.os.Bundle;


import com.jungle.mvvm.base.BaseViewModel;
import com.jungle.mvvm.sample.annotation.Authorize;
import com.jungle.mvvm.sample.app.WanObserver;
import com.jungle.mvvm.sample.bean.ArticleBean;
import com.jungle.mvvm.sample.bean.CollectBean;
import com.jungle.mvvm.sample.bean.SameBean;
import com.jungle.mvvm.sample.http.WanNetwork;
import com.jungle.mvvm.sample.ui.collect.CollectModel;
import com.jungle.mvvm.sample.ui.collect.CollectViewModel;
import com.jungle.mvvm.sample.ui.detail.DetailActivity;
import com.jungle.mvvm.utils.KLog;
import com.jungle.mvvm.utils.ToastUtils;

import io.reactivex.Observable;

/**
 * @ClassName ArticleUtil
 * @Description Article工具类
 * @Author Daniel
 * @Version V1.0.0
 */
public class ArticleUtil {
    private ArticleUtil() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static void toDetail(String title, String url, BaseViewModel viewModel) {
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        bundle.putString("title", title);
        viewModel.startActivity(DetailActivity.class, bundle);
    }

    @SuppressLint("CheckResult")
    @Authorize
    public static void toCollect(ArticleBean articleBean, ObservableField<Boolean> hasCollect, BaseViewModel viewModel) {
        Observable<SameBean> collect;
        boolean isCollect = articleBean.getCollect();
        if (isCollect) {
            collect = CollectModel.uncollect(articleBean.getId());
        } else {
            collect = CollectModel.collect(articleBean.getId());
        }
        collect
                .compose(WanNetwork.applyService(viewModel.getLifecycleProvider(), new WanObserver<SameBean>() {
                    @Override
                    public void onWanSuccess(SameBean sameBean) {
                        ToastUtils.showShort(isCollect ? "取消成功" : "收藏成功");
                        articleBean.setCollect(!isCollect);
                        hasCollect.set(!isCollect);
                    }

                    @Override
                    public void onWanFailure(String e) {
                        KLog.e(e);
                    }
                }));
    }

    @SuppressLint("CheckResult")
    @Authorize
    public static void toCollect(CollectBean collectBean, int position, CollectViewModel viewModel) {
        Observable<SameBean> collect = CollectModel.uncollect(collectBean.getId(), collectBean.getOriginId());

        collect
                .compose(WanNetwork.applyService(viewModel.getLifecycleProvider(), new WanObserver<SameBean>() {
                    @Override
                    public void onWanSuccess(SameBean sameBean) {
                        ToastUtils.showShort("取消成功");
                        viewModel.observableList.remove(position);
                    }

                    @Override
                    public void onWanFailure(String e) {
                        KLog.e(e);
                    }
                }));
    }

    public static void openBrowser(Context context, String url) {
        final Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(Intent.createChooser(intent, "请选择浏览器"));
        } else {
            ToastUtils.showShort("链接错误或无浏览器");
        }
    }
}
